package com.jqb.ajaxlab.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class Ajax03 extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*JQuery $.post dom4j xml*/
		Document doc =  DocumentHelper.createDocument();
		Element root = doc.addElement("users");
		for(int i=1000;i<1005;i++){
			Element user = root.addElement("user");
			user.addAttribute("id", i+"");
			Element name = user.addElement("name");
			name.addText("admin"+i);
		}
		response.setHeader("pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter(out,format);
		writer.write(doc);
	}
}
